#!/usr/bin/python
#-*-coding: utf-8 -*-

#This file is part of pyLot library.
#
# pyLot is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pyLot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with pyLot.  If not, see <http://www.gnu.org/licenses/>.

__author__ = u'Pierre Puiseux, Guillaume Baty'
__copyright__ = u"Copyright 2011-2012 (C) andheo, Université de Pau et des Pays de l'Adour"
__credits__ = [u'Pierre Puiseux', u'Guillaume Baty']
__license__ = "GNU Lesser General Public License"

__all__ = [u'HandlerPart']

from pyLot.core import Handler, get_data
from operators import divide_integers

class HandlerPart (Handler):
  def __init__ (self):
    Handler.__init__(self)
    self.addOptions(get_data(__name__, u'conf.xml'), main=True)

  def _setprecision (self, value):
    assert(0 <= value <= 20)
    self.opt[u'computation'][u'precision'] = value

  precision = property(
    fget=lambda self:self.opt[u'computation'](u'precision'),
    fset=_setprecision)

  def writeDivide(self, a, b):
    res = self.divide(a, b)
    self.io.write(self.tr_(u'result of %d/%d: %s\n') % (a, b, res))

  def divide (self, a, b):
    u"""
    Computes a/b

    :param a: an integer
    :param b: an integer different from 0
    :return: a/b
    :rtype: float
    """
    return round(divide_integers(a, b, self.tr_), self.precision)

  def _readSettings(self, settings, common):
    self.precision = settings.getValue(u'sample/precision', self.precision).value

  def _writeSettings(self, settings, common):
    settings.setValue(u'sample/precision', self.precision)

  def _translate(self):
    self.alias = self.tr_(u'Sample')
    self.tooltip = self.tr_(u'A Sample Plugin')

if __name__ == '__main__':
  u"""
  Example of script
  """
  handler = HandlerPart()
  handler.init()
  handler.precision = 5
  handler.writeDivide(1, 3)
